home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Graphics / Shapes / Shapes.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  7.4 KB  |  224 lines

  1. /*******************************************************************************
  2.   ShapesDemo
  3.   Example submitted by by David Clegg
  4.  
  5.   This unit contains the base Shape class and its descendants, which are used
  6.   to render onto a GDI+ drawing surface.
  7. *******************************************************************************/
  8.  
  9. using System;
  10. using System.Drawing;
  11. using System.Drawing.Drawing2D;
  12.  
  13. namespace ShapesDemo
  14. {
  15.     /// <summary>
  16.     /// Base class for all shapes to descend from. It implements the logic for
  17.     /// drawing an outline of the shape, and for filling the shape using the
  18.     /// specified brush. It implements the IDisposable interface as the canvas
  19.     /// reference needs to be disposed
  20.     /// Concrete classes need only implement the GetShape method which should
  21.     /// return a GraphicsPath instance representing the bounds of the shape.
  22.     /// As a contrast, some concrete classes may also implement alternative
  23.     /// methods for drawing the outline and filling the shape.
  24.     /// </summary>
  25.     public abstract class Shape : IDisposable {
  26.         private Point fOldStartPoint;
  27.         private Point fOldEndPoint;
  28.         private Graphics fCanvas;
  29.         private bool fDisposed = false;
  30.  
  31.         protected Graphics Canvas {
  32.             get {return fCanvas;}
  33.         }
  34.  
  35.         public Shape(Graphics canvas)
  36.         {
  37.             fCanvas = canvas;
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Implement IDisposable.Dispose to ensure the fCanvas Graphics reference
  42.         /// is explicitly disposed.
  43.         /// </summary>
  44.         public void Dispose() {
  45.             if (!fDisposed) {
  46.                 fCanvas.Dispose();
  47.                 fDisposed = true;
  48.             }
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Destructor. Used to call .Dispose to ensure that fCanvas.Dispose
  53.         /// is called if it hasn't already been done.
  54.         /// </summary>
  55.         ~Shape() {
  56.             Dispose();
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Draws a solid shape based on the startPoint and endPoint
  61.         /// co-ordinates. Concrete classes must implement the GetShape method
  62.         /// to determine the bounds for the shape.
  63.         /// </summary>
  64.         public void Draw(Point startPoint, Point endPoint, Brush brush) {
  65.             //Draw the shape
  66.             fCanvas.FillRegion(brush, new Region(GetShape(startPoint, endPoint)));
  67.  
  68.             //As the outline has a size of 1, it will still be visible along
  69.             //some points of the shape. Draw another outline which uses a pen
  70.             //the same color as the fill if it uses a SolidBrush, or one that
  71.             //uses the same color as the original outline for other brush types.
  72.             if (brush is SolidBrush)
  73.                 DoOutline(new Pen(((SolidBrush)brush).Color, 1), startPoint, endPoint);
  74.             else
  75.                 DoOutline(new Pen(Color.Black, 1), startPoint, endPoint);
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Draws an outline of a shape based in the startPoint and endPoint
  80.         /// co-ordinates. Concrete classes must implement the GetShape method
  81.         /// to determine the bounds for the shape.
  82.         /// </summary>
  83.         public void DrawOutline(Point startPoint, Point endPoint) {
  84.             //First erase the last Outline
  85.             Pen lPen = new Pen(Color.White, 1);
  86.             DoOutline(lPen, fOldStartPoint, fOldEndPoint);
  87.  
  88.             //Draw the new Outline
  89.             lPen = new Pen(Color.Black, 1);
  90.             DoOutline(lPen, startPoint, endPoint);
  91.  
  92.             //Save the start and end points for next time
  93.             fOldStartPoint = startPoint;
  94.             fOldEndPoint = endPoint;
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Return a Rectangle structure to represent the bounds of the shape.
  99.         /// </summary>
  100.         protected System.Drawing.Rectangle GetRectangle(Point startPoint, Point endPoint) {
  101.             return System.Drawing.Rectangle.FromLTRB(startPoint.X, startPoint.Y,
  102.                                                      endPoint.X, endPoint.Y);
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Draws the outline for the shape.
  107.         /// </summary>
  108.         private void DoOutline(Pen pen, Point startPoint, Point endPoint) {
  109.             GraphicsPath lGraphicsPath = GetShape(startPoint, endPoint);
  110.             fCanvas.DrawPath(pen, lGraphicsPath);
  111.         }
  112.  
  113.         protected abstract GraphicsPath GetShape(Point startPoint, Point endPoint);
  114.     }
  115.  
  116.     /// <summary>
  117.     /// Class to draw a rectangle.
  118.     /// </summary>
  119.     public class Rectangle : Shape {
  120.         public Rectangle(Graphics canvas) : base (canvas) {}
  121.  
  122.         /// <summary>
  123.         /// Alternative method to draw a solid rectangle. Not as good as the
  124.         /// inherited Draw method as it doesn't cater for if an endPoint X or Y
  125.         /// value is less than its corresponding startPoint value.
  126.         /// </summary>
  127.         public void DrawRectangle(Brush brush, Point startPoint, Point endPoint) {
  128.             Canvas.FillRectangle(brush, GetRectangle(startPoint, endPoint));
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Alternative method to draw a rectangle outline. Not as good as the
  133.         /// inherited Draw method as it doesn't cater for if an endPoint X or Y
  134.         /// value is less than its corresponding startPoint value.
  135.         /// </summary>
  136.         public void OutlineRectangle(Pen pen, Point startPoint, Point endPoint) {
  137.             Canvas.DrawRectangle(pen, GetRectangle(startPoint, endPoint));
  138.         }
  139.  
  140.         protected override GraphicsPath GetShape(Point startPoint, Point endPoint) {
  141.             GraphicsPath lRetVal = new GraphicsPath();
  142.  
  143.             Point lTopRight = new Point(startPoint.X, endPoint.Y);
  144.             Point lBottomLeft = new Point(endPoint.X, startPoint.Y);
  145.  
  146.             lRetVal.AddLine(startPoint, lTopRight);
  147.             lRetVal.AddLine(lTopRight, endPoint);
  148.             lRetVal.AddLine(endPoint, lBottomLeft);
  149.             lRetVal.AddLine(lBottomLeft, startPoint);
  150.             return lRetVal;
  151.         }
  152.     }
  153.  
  154.     /// <summary>
  155.     /// Class to draw an ellipse.
  156.     /// </summary>
  157.     public class Ellipse: Shape {
  158.         public Ellipse(Graphics canvas) : base (canvas){}
  159.  
  160.         protected override GraphicsPath GetShape(Point startPoint, Point endPoint) {
  161.             GraphicsPath lRetVal = new GraphicsPath();
  162.             lRetVal.AddEllipse(GetRectangle(startPoint, endPoint));
  163.             return lRetVal;
  164.         }
  165.  
  166.         public void DrawEllipse(Brush brush, Point startPoint, Point endPoint) {
  167.             Canvas.FillEllipse(brush, GetRectangle(startPoint, endPoint));
  168.         }
  169.  
  170.         /// <summary>
  171.         /// Alternative method to draw an Elipse outline. Not as good as the
  172.         /// inherited Draw method as it doesn't cater for if an endPoint X or Y
  173.         /// value is less than its corresponding startPoint value.
  174.         /// </summary>
  175.         public void OutlineEllipse(Pen pen, Point startPoint, Point endPoint) {
  176.             Canvas.DrawEllipse(pen, GetRectangle(startPoint, endPoint));
  177.         }
  178.     }
  179.  
  180.     /// <summary>
  181.     /// Class to draw a triangle.
  182.     /// </summary>
  183.     public class Triangle: Shape {
  184.         public Triangle(Graphics canvas) : base (canvas){}
  185.  
  186.         /// <summary>
  187.         /// Create a GraphicsPath object representing the bounds for the triangle
  188.         /// </summary>
  189.         protected override GraphicsPath GetShape(Point startPoint, Point endPoint) {
  190.             GraphicsPath lRetVal = new GraphicsPath();
  191.  
  192.             Point lStartPoint = new Point(endPoint.X - ((endPoint.X - startPoint.X) / 2), startPoint.Y);
  193.             Point lBottomLeft = new Point(startPoint.X, endPoint.Y);
  194.  
  195.             lRetVal.AddLine(lStartPoint, endPoint);
  196.             lRetVal.AddLine(endPoint, lBottomLeft);
  197.             lRetVal.AddLine(lBottomLeft, lStartPoint);
  198.             return lRetVal;
  199.         }
  200.     }
  201.  
  202.     /// <summary>
  203.     /// Class to draw a Line.
  204.     /// </summary>
  205.     public class Line: Shape {
  206.         public Line(Graphics canvas) : base (canvas){}
  207.  
  208.         /// <summary>
  209.         /// Create a GraphicsPath object representing the bounds for the Line
  210.         /// </summary>
  211.         protected override GraphicsPath GetShape(Point startPoint, Point endPoint) {
  212.             GraphicsPath lRetVal = new GraphicsPath();
  213.             lRetVal.AddLine(startPoint, endPoint);
  214.             return lRetVal;
  215.         }
  216.  
  217.         public void DrawLine(Pen pen, Point startPoint, Point endPoint) {
  218.             Canvas.DrawLine(pen, startPoint, endPoint);
  219.         }
  220.     }
  221.  
  222.  
  223. }
  224.